package com.lcy.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lcy.reggie.common.Result;
import com.lcy.reggie.entity.Category;
import com.lcy.reggie.service.CategoryService;
import com.lcy.reggie.mapper.CategoryMapper;
import com.lcy.reggie.service.DishService;
import com.lcy.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
* @author ChunBaBa_
* @description 针对表【category(菜品及套餐分类)】的数据库操作Service实现
* @createDate 2023-01-16 22:55:26
*/
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category>
    implements CategoryService{
    @Autowired
    private DishService dishService;

    @Autowired
    private SetmealService setmealService;


    @Autowired
    private CategoryMapper categoryMapper;

    /**
     * 新增菜品/套餐分类
     * @param category
     * @return
     */
    @Override
    public Result<String> addCategory(Category category) {
        //1.写入数据库   (这里本来该先判断name是否重复，但是我们之前设置了一个全局异常捕获，所以，可以把异常写在那里)
        boolean save = save(category);

        if(!save){
            return Result.error("添加失败！");
        }
        return Result.success("添加成功！");
    }

    /**
     * 分页查询
     */
    @Override
    public Result<Page> pageResult(HttpServletRequest request, Integer page, Integer pageSize) {
        //1.构建分页构造器
        Page pageInfo = new Page(page,pageSize);
        //2.分页查询
        query().orderByAsc("sort").page(pageInfo);
        return Result.success(pageInfo);
    }

    /**
     * 删除功能
     * @param id
     * @return
     */
    @Override
    public Result<String> delete(Long id) {
        //1.判断是否已经关联其他菜品或套餐
        Long count = dishService.query().eq("category_id", id).count();
        Long count2 = setmealService.query().eq("category_id", id).count();
        if(count>0 || count2>0){
            return Result.error("删除失败！");
        }
        //2.不存在，可以删除
        removeById(id);
        return Result.success("删除成功！");
    }

    /**
     * 修改功能
     * @param category
     * @return
     */
    @Override
    public Result<String> update(Category category) {
        //修改数据
        boolean result = updateById(category);
        if(!result){
            return Result.error("修改失败！");
        }
        return Result.success("修改成功！");
    }


    /**
     *  根据条件查询分类数据
     * @param category
     * @return
     */
    @Override
    public Result<List<Category>> list(Category category) {
        //条件构造器
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        //添加条件
        queryWrapper.eq(category.getType() != null,Category::getType,category.getType());
        //添加排序条件
        queryWrapper.orderByAsc(Category::getSort).orderByDesc(Category::getUpdateTime);

        List<Category> list = this.list(queryWrapper);
        return Result.success(list);
    }

}




